#!/bin/bash
# 1.ip info
ip_info=`/sbin/ifconfig  | grep 'inet '| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $2}'`
echo "ip=="$ip_info > /tmp/server_monitor_tmp.log


# 2.cpu info
cpu_cores=`cat /proc/cpuinfo | grep 'cpu cores' | wc -l`
cpu_info=`uptime | awk -F'average:' '{print $2}' | awk '{print $1}'`
cpu_used=`top -b -n1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' '{split($1, vs, ","); v=vs[length(vs)]; sub(/\s+/, "", v);sub(/\s+/, "", v); printf "%d", 100-v;}'`
cpu_to_load=`echo | awk "{print $cpu_used / 100}"`
echo "cpu=="$cpu_info$cpu_to_load",,"$cpu_cores >>  /tmp/server_monitor_tmp.log

# 3.mem info
total_mem=`cat /proc/meminfo | grep MemTotal | awk '{ print $2 }'`
available_mem=`cat /proc/meminfo | grep MemAvailable | awk '{ print $2 }'`
free_mem=$[$[$total_mem - $available_mem]/1024]
total_mem=$[$total_mem/1024]
echo "mem_info=="$free_mem","$total_mem >> /tmp/server_monitor_tmp.log

# 4.harddisk info
df_info=`df -m  | grep -v "tmpfs" | awk '{print $5}'`
echo "df=="$df_info >> /tmp/server_monitor_tmp.log

# 5.netstat
net1=`grep eth /proc/net/dev | awk '{sum+=$10}END{print sum}'`
sleep 8
net2=`grep eth /proc/net/dev | awk '{sum+=$10}END{print sum}'`
net3=`expr $net2 - $net1`
echo "net=="$net3 >> /tmp/server_monitor_tmp.log

# 6.process number
nginx_num=`ps -ef | grep nginx | grep -v master | grep -v grep | wc -l`
ssdb_num=`ps -ef | grep ssdb-server | grep start | wc -l`
echo "pids==nginx("$nginx_num")+ssdb("$ssdb_num")" >> /tmp/server_monitor_tmp.log

# 7.错误日志增量
today=`date "+%Y%m%d"`
log_root='/usr/local/openresty/nginx/logs/'
last_file='/dev/shm/__last_error_file_size.log'
last_size=`cat ${last_file}`
this_size=`stat -c '%s'  ${log_root}*err*.log ${log_root}miniw/error_${today}.log  | awk '{sum+=$1}END{print sum}'`
echo ${this_size} > ${last_file}
echo "errsize=="`expr $this_size - $last_size` >> /tmp/business_monitor.log
echo "errtotal=="$this_size >> /tmp/business_monitor.log

# 8.name info
echo "name==cn-business_advert-logic[01]" >> /tmp/server_monitor_tmp.log

#*/1 * * * * sh /home/server_monitor.sh >/dev/null 2>&1
cat  /tmp/server_monitor_tmp.log
wget 'http://120.24.64.132:8080/oa/monitor.php?server_type=cn-activity&cmd=set' --post-file=/tmp/server_monitor_tmp.log  -O /tmp/server_monitor_wget.log -q
